//
//  Concentration.swift
//  test
//
//  Created by zoushaoshao on 2018/11/29.
//  Copyright © 2018 TT. All rights reserved.
//

import Foundation

class Concentration {
    
    var cards = [Card]()
    var indexOfOneAndOnlyFaceUpCard : Int?
    
    func chooseCard(cardIndex : Int) {
        if !cards[cardIndex].isMatched{
            if let cardNumber = indexOfOneAndOnlyFaceUpCard, cardIndex != indexOfOneAndOnlyFaceUpCard{
                if cards[cardNumber].identifier == cards[cardIndex].identifier{

                    cards[cardNumber].isMatched = true
                    cards[cardIndex].isMatched = true
                }
                
                indexOfOneAndOnlyFaceUpCard = nil
                cards[cardIndex].isFaceUp = true
                
            }else{
                // 或者没有翻牌，或者有两个翻牌
                for index in cards.indices{
                    cards[index].isFaceUp = false
                    
                }
                cards[cardIndex].isFaceUp = true
                indexOfOneAndOnlyFaceUpCard = cardIndex
            }
            
        }
        
    }
    
    init(numberOfPairsOfCard : Int) {
        for _ in 0...numberOfPairsOfCard {
            
            let card = Card()
            
            cards += [card,card]
        }
    }

}


